package com.example.landmanagement.controller;

import com.example.landmanagement.common.exception.MVCException;
import com.example.landmanagement.pojo.adminInfo;
import com.example.landmanagement.service.adminInfoService;
import com.example.landmanagement.util.CodeEnum;
import com.example.landmanagement.util.MD5Tool;
import com.example.landmanagement.util.ReturnBeanCom;
import com.example.landmanagement.util.ReturnBeanSim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class adminController {

    @Autowired
    private adminInfoService adminInfoService;
    //登录
    @PostMapping("/login")
    public ResponseEntity loginAdmin(@RequestBody adminInfo admin) throws MVCException{
        String adminpwd = admin.getAdminpwd();
        String encodeValue = MD5Tool.encodeValue(adminpwd, "utf-8");
        admin.setAdminpwd(encodeValue);
        adminInfo adminInfo = adminInfoService.login(admin);
        if (adminInfo!=null){
            return  ResponseEntity.ok(new ReturnBeanCom(CodeEnum.LOGIN_SUC,adminInfo));

        }
        else {
            throw new MVCException(CodeEnum.ERROR);
        }

    }
    //注册
    @PutMapping("/add")
    public ResponseEntity addAdmin(@RequestBody adminInfo admin) throws Exception{
        String adminpwd = admin.getAdminpwd();
        String encodeValue = MD5Tool.encodeValue(adminpwd, "utf-8");
        admin.setAdminpwd(encodeValue);
        boolean isAdd =  adminInfoService.add(admin);
        if (isAdd){
            return ResponseEntity.ok(new ReturnBeanSim(CodeEnum.SUCCESS));
        }else {
            throw new MVCException(CodeEnum.ERROR);
        }

    }

    public ResponseEntity updateAdmin(@RequestBody adminInfo admin) throws Exception{
        String adminpwd = admin.getAdminpwd();
        String encodeValue = MD5Tool.encodeValue(adminpwd, "utf-8");
        admin.setAdminpwd(encodeValue);
        return null;
    }

}
